package com.greenantmall.activity;

import com.greenantmall.R;
import com.greenantmall.bean.ShippingAddr;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.utils.iLog;
import com.lsarah.utils.SystemBarTintManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MyAddressEditActivity extends BaseActivity
{

    private final static String TAG = "MyAddressEditActivity";

    public static final String KEY_MODE = "key_mode";
    public static final String KEY_ADDRESS_NAME = "KEY_ADDRESS_NAME";
    public static final String KEY_ADDRESS_MOBILE = "KEY_ADDRESS_MOBILE";
    public static final String KEY_ADDRESS_UPTOWN = "KEY_ADDRESS_UPTOWN";
    public static final String KEY_ADDRESS_DOOR = "KEY_ADDRESS_DOOR";
    public static final String KEY_ADDRESS_OBJECTID = "KEY_ADDRESS_OBJECTID";

    public static final int MODE_ADD = 0;
    public static final int MODE_EDIT = 1;
    public static final int MODE_CHOOSE = 2;

    private EditText name;
    private EditText mobile;
    private EditText uptown;
    private EditText door;
    private TextView add;
    private TextView iv_title;
    private Context mContext;
    private int mode = MODE_ADD;
    
    private String objectId;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mContext = this;
        SystemBarTintManager.setContentView(this, R.layout.myaddressedit, getResources().getColor(R.color.green));
        
        name = (EditText) this.findViewById(R.id.name);
        mobile = (EditText) this.findViewById(R.id.mobile);
        uptown = (EditText) this.findViewById(R.id.uptown);
        door = (EditText) this.findViewById(R.id.door);
        uptown.setText(UtilTools.getServerSection(getApplication()).getAddress());
        add = (TextView) this.findViewById(R.id.add);
        iv_title = (TextView) this.findViewById(R.id.iv_title);
        Intent intent = getIntent();
        if (intent != null)
        {
            Bundle bundle = intent.getExtras();
            if (bundle != null)
            {
                mode = bundle.getInt(KEY_MODE, MODE_ADD);
                if (mode == MODE_EDIT)
                {
                    add.setText(R.string.modify);
                    iv_title.setText(R.string.menu_address_edit);
                    objectId=bundle.getString(KEY_ADDRESS_OBJECTID);
                    name.setText(bundle.getString(KEY_ADDRESS_NAME));
                    mobile.setText(bundle.getString(KEY_ADDRESS_MOBILE));
                    uptown.setText(bundle.getString(KEY_ADDRESS_UPTOWN));
                    door.setText(bundle.getString(KEY_ADDRESS_DOOR));
                }
            }
        }

        add.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                if (!checkIfIsCorrect())
                {
                    UtilTools.showMyToast(mContext, mContext.getString(R.string.failed_hint));
                    return;
                }

                if (mode == MODE_EDIT)
                {
                    RetrieveDataAsyn.getInstance().updateAddress(objectId, getUserId(),
                        name.getText().toString(), mobile.getText().toString(), uptown.getText().toString(),
                        door.getText().toString(), 0, new RetrieveDataListener()
                    {

                        @Override
                        public void dataRetrieved(int requestId, Object data)
                        {
                            if (data == null)
                            {
                                UtilTools.showMyToast(mContext, mContext.getString(R.string.failed));
                            } else
                            {
                                UtilTools.showMyToast(mContext, mContext.getString(R.string.success));
                                finish();
                            }
                        }
                    });
                } else
                {
                    RetrieveDataAsyn.getInstance().addAddress(getUserId(), name.getText().toString(),
                        mobile.getText().toString(), uptown.getText().toString(), door.getText().toString(), 0,
                        new RetrieveDataListener()
                    {

                        @Override
                        public void dataRetrieved(int requestId, Object data)
                        {
                            if (data == null)
                            {
                                UtilTools.showMyToast(mContext, mContext.getString(R.string.failed));
                            } else
                            {
                                UtilTools.showMyToast(mContext, mContext.getString(R.string.success));
                                if(mode==MODE_CHOOSE)
                                {
                                    Intent intent = new Intent();
                                    Bundle bundle = new Bundle();
                                    bundle.putString(MyAddressEditActivity.KEY_ADDRESS_NAME, name.getText().toString());
                                    bundle.putString(MyAddressEditActivity.KEY_ADDRESS_MOBILE,
                                        mobile.getText().toString());
                                    bundle.putString(MyAddressEditActivity.KEY_ADDRESS_UPTOWN,
                                        uptown.getText().toString());
                                    bundle.putString(MyAddressEditActivity.KEY_ADDRESS_DOOR, door.getText().toString());

                                    intent.putExtras(bundle);
                                    setResult(0, intent);

                                }
                                finish();
                            }
                        }
                    });
                }

            }
        });
    }

    private boolean checkIfIsCorrect()
    {

        if (TextUtils.isEmpty(name.getText().toString())
            || TextUtils.isEmpty(mobile.getText().toString())
            || TextUtils.isEmpty(uptown.getText().toString())
            || TextUtils.isEmpty(door.getText().toString())
            ||!TextUtils.isDigitsOnly(mobile.getText().toString()))
        {
            return false;
        }
        return true;
    }

    public void goBack(View view)
    {
        finish();
    }

}
